package one.stand.service.impl;

import one.stand.dto.EquCheckDto;
import one.stand.mapper.CompanyEquMapperExt;
import one.stand.model.CompanyEqu;
import one.stand.service.CompanyEquService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.Objects;

/**
 * @author cjr
 */
@Service
public class CompanyEquServiceImpl implements CompanyEquService {

    @Autowired
    private CompanyEquMapperExt companyEquMapperExt;

    @Override
    public void delete(Integer id) {
        companyEquMapperExt.deleteByPrimaryKey(id);
    }

    @Override
    public void updatePass(Integer id, Integer companyId, String memo) {
        Assert.notNull(id);
        Assert.notNull(companyId, "公司ID不能为空");
        CompanyEqu companyEqu = new CompanyEqu();
        companyEqu.setId(id);
        companyEqu.setStatus(STATUS_PASS);
        companyEqu.setAuditTime(new Date());
        companyEqu.setCompanyId(companyId);
        companyEqu.setMemo(memo);
        companyEquMapperExt.updateByPrimaryKeySelective(companyEqu);
    }

    @Override
    public void updateRefuse(Integer id, String memo) {
        Assert.isTrue(StringUtils.isNoneBlank(memo), "备注不能为空");
        CompanyEqu companyEqu = new CompanyEqu();
        companyEqu.setId(id);
        companyEqu.setStatus(STATUS_REFUSE);
        companyEqu.setMemo(memo);
        companyEquMapperExt.updateByPrimaryKeySelective(companyEqu);
    }

    @Override
    public void updateMemo(Integer id, String memo) {
        Assert.isTrue(StringUtils.isNoneBlank(memo), "备注不能为空");
        CompanyEqu companyEqu = new CompanyEqu();
        companyEqu.setId(id);
        companyEqu.setMemo(memo);
        companyEquMapperExt.updateByPrimaryKeySelective(companyEqu);
    }

    @Override
    @Transactional
    public void updateLastUserId(String equCode, int type, Integer userId) {
        CompanyEqu companyEqu = companyEquMapperExt.selectByUserIdEqu(equCode, type);
        companyEqu.setLastUserId(userId);
        companyEqu.setUpdateTime(null);
        companyEquMapperExt.updateByPrimaryKey(companyEqu);
    }

    @Override
    public EquCheckDto check(String equCode, int type) {
        // 检查是否存在
        CompanyEqu companyEqu = companyEquMapperExt.selectByUserIdEqu(equCode, type);
        if (Objects.isNull(companyEqu)) {// 不存在时添加记录
            companyEqu = new CompanyEqu();
            companyEqu.setEquCode(equCode);
            companyEqu.setType(type);
            companyEqu.setStatus(STATUS_NO);
            companyEqu.setCreateTime(new Date());
            companyEquMapperExt.insert(companyEqu);
            return EquCheckDto.builder().build();
        }
        // 检查
        if (companyEqu.getStatus() == STATUS_PASS) {
            return EquCheckDto.builder()
                    .companyId(companyEqu.getCompanyId())
                    .lastUserId(companyEqu.getLastUserId())
                    .build();
        } else {
            return EquCheckDto.builder().build();
        }
    }
}
